package alipay.openplatform.edu.campus.jiuye.service.services.topic;

import alipay.openplatform.edu.campus.jiuye.common.utils.CopyVoPo;
import alipay.openplatform.edu.campus.jiuye.common.utils.ObjectUtils;
import alipay.openplatform.edu.campus.jiuye.dao.mappers.topic.TopicSchoolInfoMapper;
import alipay.openplatform.edu.campus.jiuye.model.po.TopicSchoolInfoPO;
import alipay.openplatform.edu.campus.jiuye.model.vo.SchoolVO;
import alipay.openplatform.edu.campus.jiuye.service.iservice.topic.ITopicSchoolInfoService;
import alipay.openplatform.edu.campus.jiuye.service.services.system.AreaInfoService;
import alipay.openplatform.edu.campus.jiuye.service.services.system.SchoolInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 主题-学校表的相关操作
 */
@Service
public class TopicSchoolInfoService implements ITopicSchoolInfoService {

    //日志操作类
    private static Logger logger = LoggerFactory.getLogger(AreaInfoService.class);

    @Autowired
    private TopicSchoolInfoMapper topicSchoolInfoMapper;

    @Autowired
    private SchoolInfoService schoolInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSchool(int topicId,List<SchoolVO> schoolVOs) {
        List<TopicSchoolInfoPO> topicSchoolInfoPOs = changeVOToPO(topicId, schoolVOs);
        topicSchoolInfoMapper.insert(topicSchoolInfoPOs);
//        return topicSchoolInfoPOs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSchool(Integer topicId, List<SchoolVO> schoolVOs) {
        //清空信息
        topicSchoolInfoMapper.deleteByTopicId(topicId);
        List<TopicSchoolInfoPO> topicSchoolInfoPOs = changeVOToPO(topicId, schoolVOs);
        topicSchoolInfoMapper.insert(topicSchoolInfoPOs);
//        return topicSchoolInfoPOs;
    }

    private List<TopicSchoolInfoPO> changeVOToPO(int topicId,List<SchoolVO> schoolVOs){
        List<TopicSchoolInfoPO> topicSchoolInfoPOs = new ArrayList<TopicSchoolInfoPO>();
        for (SchoolVO schoolVO : schoolVOs) {
            if (ObjectUtils.isNull(schoolVO)){
                continue;
            }
            TopicSchoolInfoPO topicSchoolInfoPO = new TopicSchoolInfoPO();
//            Long cityId = schoolInfoService.getCityIdBySchoolZoneCode(schoolVO.getSchoolZoneCode());
//            if (ObjectUtils.isNull(cityId)){
//                logger.error("参数异常:TopicSchoolInfoService|changeVOToPO|schoolVO={},cityId={}", JSON.toJSONString(schoolVO),cityId);
//                throw new OnlineRuntimeException("PARAM_ERROR","根据schoolZoneCode查询cityId异常!");
//            }
            topicSchoolInfoPO.setCityId(String.valueOf(schoolVO.getCityId()));
            topicSchoolInfoPO.setTopicId(topicId);
            topicSchoolInfoPO.setSchoolId(schoolVO.getSchoolId());
            topicSchoolInfoPO.setSchoolName(schoolVO.getSchoolName());
            topicSchoolInfoPO.setId(schoolVO.getId());
//            topicSchoolInfoPO.setSchoolZoneCode(schoolVO.getSchoolZoneCode());
            topicSchoolInfoPOs.add(topicSchoolInfoPO);
        }
        return topicSchoolInfoPOs;
    }

    /**
     * 根据学校id查询学校信息
     *
     * @param schoolId 学校id
     * @param cityId  城市id
     * @return
     */
    @Override
    public List<TopicSchoolInfoPO> queryByschoolId(String schoolId,String cityId) {
        return topicSchoolInfoMapper.queryByschoolId(schoolId,cityId);
    }

    @Override
    public List<SchoolVO> getSchoolsByTopicId(Integer topicId) {
        List<TopicSchoolInfoPO> topicSchoolInfoPOs = topicSchoolInfoMapper.selectByTopicId(topicId);
        return CopyVoPo.copyList(topicSchoolInfoPOs,SchoolVO.class);
    }

    @Override
    public void deleteSchoolByTopicId(Integer topicId) {
        topicSchoolInfoMapper.deleteByTopicId(topicId);
    }
}